###############################################################################
# SuperLinks                                                                  #
# Copyright (c) 2000-2007 Greg Nolle (http://greg.nolle.co.uk)                #
###############################################################################
# This program is free software; you can redistribute it and/or modify it     #
# under the terms of the GNU General Public License as published by the Free  #
# Software Foundation; either version 2 of the License, or (at your option)   #
# any later version.                                                          #
#                                                                             #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       #
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for   #
# more details.                                                               ##                                                                             #
# You should have received a copy of the GNU General Public License along     #
# with this program; if not, write to the Free Software Foundation, Inc.,     #
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.                 #
###############################################################################
# ControlPanel/ModifyStyleSheet.pm.pl -> ModifyStyleSheet module              #
###############################################################################
# DON'T EDIT BELOW UNLESS YOU KNOW WHAT YOU'RE DOING!                         #
###############################################################################
package CP::ModifyStyleSheet;

BEGIN { require "System.pm.pl"; import System qw($SYSTEM); }
BEGIN { require "General.pm.pl"; import General qw($GENERAL); }
require "Error.pm.pl";
require "Standard.pm.pl";
require "Authenticate.pm.pl";
require "HashToString.pm.pl";
require "StaticPages.pm.pl";

use strict;

require "ControlPanel/Output/ModifyStyleSheet.pm.pl";

sub new {
  my ($class) = @_;
  my $self    = {};
  return bless($self, $class);
}

sub DESTROY { }

###############################################################################
# show subroutine
sub show {
  my $self = shift;
  my %in = (DB => undef, ERROR => "", @_);

  #----------------------------------------------------------------------#
  # Authenticating...                                                    #

  &Authenticate::CP(DB => $in{'DB'}, ACTION => "STYLESHEETS_MODIFY");

  #----------------------------------------------------------------------#
  # Checking fields...                                                   #

  if (!$SL::QUERY{'SSID'}) {
    &Error::Error("CP", MESSAGE => "You didn't specify a StyleSheet ID (SSID)");
  }
  
  my $StyleSheet = $in{'DB'}->BinarySelect(
    TABLE => "StyleSheets",
    KEY   => $SL::QUERY{'SSID'}
  );
  unless ($StyleSheet) {
    &Error::Error("CP", MESSAGE => "The StyleSheet ID (SSID) you specified is invalid");
  }

  $StyleSheet->{'stylesheet'} = eval($StyleSheet->{'STYLESHEET'});
  
  my %INPUT;
  
  $INPUT{'STYLESHEET'}  = $StyleSheet;

  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  my $Skin = Skin::CP::ModifyStyleSheet->new();

  &Standard::PrintHTMLHeader();
  print $Skin->show(input => \%INPUT, error => $in{'ERROR'});
  
  return 1;
}

###############################################################################
# do subroutine
sub do {
  my $self = shift;
  my %in = (DB => undef, @_);

  #----------------------------------------------------------------------#
  # Authenticating...                                                    #

  &Authenticate::CP(DB => $in{'DB'}, ACTION => "STYLESHEETS_MODIFY");

  #----------------------------------------------------------------------#
  # Checking fields...                                                   #

  if (!$SL::QUERY{'SSID'}) {
    &Error::Error("CP", MESSAGE => "You didn't specify a StyleSheet ID (SSID)");
  }
  
  my $StyleSheet = $in{'DB'}->BinarySelect(
    TABLE => "StyleSheets",
    KEY   => $SL::QUERY{'SSID'}
  );
  unless ($StyleSheet) {
    &Error::Error("CP", MESSAGE => "The StyleSheet ID (SSID) you specified is invalid");
  }

  $StyleSheet->{'stylesheet'} = eval($StyleSheet->{'STYLESHEET'});

  my @Fields = (
    { name => "BODY_PADDING"      , required => 1 },
    { name => "BODY_IMAGE"        , required => 0 },
    { name => "BODY_BGCOLOR"      , required => 1 },
    { name => "FONT_COLOR"        , required => 1 },
    { name => "FONT_SIZE"         , required => 1 },
    { name => "FONT_FACE"         , required => 1 },
    { name => "FONT_ERROR"        , required => 1 },
    { name => "LINK_COLOR"        , required => 1 },
    { name => "LINK_VCOLOR"       , required => 1 },
    { name => "LINK_HCOLOR"       , required => 1 },
    { name => "LINK_ACOLOR"       , required => 1 },
    { name => "MAIN_WIDTH"        , required => 1 },
    { name => "MAIN_BORDER"       , required => 1 },
    { name => "MAIN_BGCOLOR"      , required => 1 },
    { name => "MAIN_ROWBORDER"    , required => 1 },
    { name => "TITLE_BGCOLOR"     , required => 1 },
    { name => "TITLE_SIZE"        , required => 1 },
    { name => "TITLE_COLOR"       , required => 1 },
    { name => "MENU_BGCOLOR"      , required => 1 },
    { name => "MENU_SIZE"         , required => 1 },
    { name => "MENU_COLOR"        , required => 1 },
    { name => "LOCATION_SIZE"     , required => 1 },
    { name => "LOCATION_COLOR"    , required => 1 },
    { name => "HEADER_IMAGE"      , required => 0 },
    { name => "HEADER_BGCOLOR"    , required => 1 },
    { name => "HEADER_SIZE"       , required => 1 },
    { name => "HEADER_COLOR"      , required => 1 },
    { name => "DIALOG_BGCOLOR"    , required => 1 },
    { name => "INNER_BORDER"      , required => 1 },
    { name => "INNER_BGCOLOR"     , required => 1 },
    { name => "IHEADER_BGCOLOR"   , required => 1 },
    { name => "IHEADER_SIZE"      , required => 1 },
    { name => "IHEADER_COLOR"     , required => 1 },
    { name => "ROW_BGCOLOR"       , required => 1 },
    { name => "ROW_SIZE"          , required => 1 },
    { name => "ROW_COLOR"         , required => 1 },
    { name => "FOOTER_BGCOLOR"    , required => 1 },
    { name => "FOOTER_SIZE"       , required => 1 },
    { name => "FOOTER_COLOR"      , required => 1 },
    { name => "BOTTOMBAR_HEIGHT"  , required => 1 },
    { name => "BOTTOMBAR_BGCOLOR" , required => 1 },
    { name => "REQUIRED_SIZE"     , required => 1 },
    { name => "REQUIRED_COLOR"    , required => 1 },
    { name => "SUBJECT_SIZE"      , required => 1 },
    { name => "SUBJECT_COLOR"     , required => 1 },
    { name => "FIELD_SIZE"        , required => 1 },
    { name => "FIELD_COLOR"       , required => 1 },
    { name => "LABEL_SIZE"        , required => 1 },
    { name => "LABEL_COLOR"       , required => 1 },
    { name => "FORM_FACE"         , required => 1 },
    { name => "FORM_BGCOLOR"      , required => 0 },
    { name => "FORM_SIZE"         , required => 1 },
    { name => "FORM_COLOR"        , required => 1 },
    { name => "FORM_WIDTH"        , required => 1 },
    { name => "DIVIDER_BGCOLOR1"  , required => 1 },
    { name => "DIVIDER_BGCOLOR2"  , required => 1 }
  );

  my (%RECORD, %INPUT);

  my @Error;
  foreach my $field (@Fields) {
    if ($field->{'required'} && $SL::QUERY{'FORM_'.$field->{'name'}} eq "") {
      push(@Error, "MISSING-".$field->{'name'});
    } elsif ($field->{'size'} && $SL::QUERY{'FORM_'.$field->{'name'}} ne "" && length($SL::QUERY{'FORM_'.$field->{'name'}}) > $field->{'size'}) {
      push(@Error, "TOOLONG-".$field->{'name'});
    } else {
      $RECORD{ $field->{'name'} } = $SL::QUERY{'FORM_'.$field->{'name'}};
    }
  }
  
  push(@Error, "MISSING-DESCRIPTION") if ($SL::QUERY{'FORM_DESCRIPTION'} eq "");
  push(@Error, "TOOLONG-DESCRIPTION") if (length($SL::QUERY{'FORM_DESCRIPTION'}) > 256);
  
  if (scalar(@Error) >= 1) {
    $self->show(DB => $in{'DB'}, ERROR => \@Error);
    return 1;
  }

  #----------------------------------------------------------------------#
  # Updating data...                                                     #

  my $HashToString = HashToString->new();
  
  (
    $in{'DB'}->Update(
      TABLE   => "StyleSheets",
      VALUES  => {
        DESCRIPTION => $SL::QUERY{'FORM_DESCRIPTION'},
        STYLESHEET  => $HashToString->Write(\%RECORD)
      },
      KEY     => $SL::QUERY{'SSID'}
    )
  ) || &Error::Error("CP", MESSAGE => "Error updating record. $in{'DB'}->{'ERROR'}");
  
  $RECORD{'ID'}          = $SL::QUERY{'SSID'};
  $RECORD{'DESCRIPTION'} = $SL::QUERY{'FORM_DESCRIPTION'};

  my $StaticPages = StaticPages->new(DB => $in{'DB'});
     $StaticPages->BuildStyleSheet($RECORD{'ID'})
       || &Error::Error("CP", MESSAGE => "Error updating static pages. $StaticPages->{'ERROR'}");

  $INPUT{'RECORD'}     = \%RECORD;
  $INPUT{'STYLESHEET'} = $StyleSheet;

  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  my $Skin = Skin::CP::ModifyStyleSheet->new();

  &Standard::PrintHTMLHeader();
  print $Skin->do(input => \%INPUT);
  
  return 1;
}

1;